package cn.yunbamboos.media.hook;

import cn.yunbamboos.media.core.MediaService;
import cn.yunbamboos.media.hook.in.GenericHookInDTO;
import cn.yunbamboos.media.hook.in.ServerExitedHookInDTO;
import cn.yunbamboos.media.hook.in.ServerKeepaliveHookInDTO;
import cn.yunbamboos.media.hook.in.ServerStartedHookInDTO;
import cn.yunbamboos.media.hook.in.StreamNotFoundHookInDTO;
import cn.yunbamboos.media.hook.out.GenericHookOutDTO;
import cn.yunbamboos.media.hook.out.IHookOutDTO;
import cn.yunbamboos.media.hook.out.StreamNoneReaderHookOutDTO;
import cn.yunbamboos.media.zlm.ZLMClientList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;

public class MediaHookService {

    private static final Logger logger = LoggerFactory.getLogger(MediaHookService.class);

    private final ZLMClientList clientList;
    private final MediaService mediaService;

    public MediaHookService(ZLMClientList clientList, MediaService mediaService) {
        this.clientList = clientList;
        this.mediaService = mediaService;
    }

    public IHookOutDTO onFlowReport(GenericHookInDTO in) {
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onHttpAccess(GenericHookInDTO in){
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onPlay(GenericHookInDTO in) {
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onPublish(GenericHookInDTO in) {
        return new GenericHookOutDTO();
    }

     public IHookOutDTO onRecordMp4(GenericHookInDTO in) {
            return new GenericHookOutDTO();
        }

     public IHookOutDTO onRtspAuth(GenericHookInDTO in) {
            return new GenericHookOutDTO();
        }

     public IHookOutDTO onRtspRealm(GenericHookInDTO in) {
            return new GenericHookOutDTO();
        }

     public IHookOutDTO onShellLogin(GenericHookInDTO in) {
            return new GenericHookOutDTO();
        }

    public IHookOutDTO onStreamChanged(GenericHookInDTO in) {
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onStreamNoneReader(GenericHookInDTO in) {
        StreamNoneReaderHookOutDTO out = new StreamNoneReaderHookOutDTO();
        out.setClose(true);
        return out;
    }

    public IHookOutDTO onStreamNotFound(StreamNotFoundHookInDTO in) {
        Optional<String> optional = in.getMediaServerId();
        if (optional.isEmpty()) {
            return GenericHookOutDTO.error("mediaServerId is empty");
        }
        String mediaServerId = optional.get();
        String app = in.getApp();
        String stream = in.getStream();
        logger.info("[流未找到] mediaServerId:{} {} {}", mediaServerId, app, stream);
        if(mediaService.play(mediaServerId, app, stream)){
            return new GenericHookOutDTO();
        }
        return GenericHookOutDTO.error("play fail");
    }

    public IHookOutDTO onRtpServerTimeout(GenericHookInDTO in) {
        return new GenericHookOutDTO();
    }


    public IHookOutDTO onServerStarted(ServerStartedHookInDTO in) {
        Optional<String> optional = in.getMediaServerId();
        if (optional.isEmpty()) {
            return new GenericHookOutDTO();
        }
        String mediaServerId = optional.get();
        // 查询到客户端
        clientList.get(mediaServerId).ifPresent(client -> {
            // 记录客户端状态
            client.setStarted(true);
        });
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onServerExited(ServerExitedHookInDTO in) {
        return new GenericHookOutDTO();
    }

    public IHookOutDTO onServerKeepalive(ServerKeepaliveHookInDTO in) {
        Optional<String> optional = in.getMediaServerId();
        if (optional.isEmpty()) {
            return new GenericHookOutDTO();
        }
        String mediaServerId = optional.get();
        // 查询到客户端
        clientList.get(mediaServerId).ifPresent(client -> {
            // 记录客户端状态
            client.setKeepaliveTime(System.currentTimeMillis());
        });
        return new GenericHookOutDTO();
    }



}
